const fs = require('fs');
const http = require('http');
const path = require('path');

var filePath = process.argv[2];
var dirPath = __dirname;
var text = '';

http.createServer((req,res)=>{
    res.writeHead(200,{"Content-Type": "text/html"});
    if(filePath){
        fs.readdir(dirPath, (err, files)=>{
            if(err){
                console.log(err);
            }else{
                var a = files.indexOf(filePath);
                if(a>-1){
                    fs.readFile(path.join(dirPath, filePath), (err, data)=>{
                        if(err){
                            console.log(err);
                        }else{
                            text = data.toString('utf-8');
                            res.write("<!DOCTYPE html><html>" + 
                            "<head></head><body>"+
                            "<div>"+ text +"</div></body></html>");
                            res.end();
                        }
                    })
                }
            }
        })
    }else{
        fs.readFile(path.join(dirPath, '/fileReader1.js'), (err, data)=>{
            text = data.toString('utf-8');
            res.write("<!DOCTYPE html><html>" + 
            "<head></head><body>"+
            "<div>"+ text +"</div></body></html>");
            res.end();
        })
    }
}).listen(8081);